﻿using System;
using System.Collections.Generic;
using System.Linq;
using MyFloridaHouse.libSvBingMapsMultiShape;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvOpenDistricts.DataService;
using MyFloridaHouse.libSvOpenDistricts.MapShapes;
using MyFloridaHouse.libSvUtilities;
using MyFloridaHouse.libSvUtilities.Logging;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;

namespace MyFloridaHouse.libSvOpenDistricts.ViewModel
{
    public partial class ViewModelBase
    {
        //TODO move GetLayers into its own Layers object? just like LayersDataDictionary
        public void OnServiceDALGetLayersCompletedEventHandler(LayerSTO sto)
        {
            InitializeLayers(sto.layerList);
            isLayersInitializeCompleted = true;
            CheckInitializedComplete();
        }



        //TODO move GetRegionData into its own RegionData object? just like LayersDataDictionary
        public void OnServiceDALGetRegionDataCompleted(RegionDataListSTO sto)
        {
            try
            {
                regionDataList.Clear();
                foreach (var rgnData in sto.regionDataList)
                {
                    regionDataList.Add(rgnData);
                }
            }
            catch (Exception ex)
            {
                Log.HandleInternalError("Problem loading region data : " + ex.Message.ToString());
            }
        
        }




        public void OnServiceDALGetRegionIDsInViewCompleted(RegionIDsSTO sto)
        {
            var mapLayer = (from lyr in mapLayerList
                            where lyr.layerID == sto.layerID
                            select lyr).First();
            if (null == mapLayer)
            {
                Log.HandleInternalError("OnServiceDALGetRegionIDsInViewCompleted: unable to find returned layerID [" + sto.layerID + "] in known map layers");
            }
            else
            {
            byte[] decompressed = CompressionManager.DecompressToByteArray((byte[])sto.compressedRegionIDList);
            List<int> regionIDList = (List<int>)SerializationManager.Deserialize(typeof(List<int>), decompressed);

                mapLayer.getShapesRequestPending = false;
                List<int> newRegionsIDsWithoutShapes = mapLayer.SaveRegionsInView(regionIDList);

            if (newRegionsIDsWithoutShapes.Count() > 0)
            {
                if (newRegionsIDsWithoutShapes.Count() > Constants.ShapeServiceMaxRetrieveCount)
                {

                    Log.HandleInternalError(mapLayer.layerName + " zoom level settings would have caused the retrieval of [" + newRegionsIDsWithoutShapes.Count.ToString() + "] shapes, which is over the threshold of " + Constants.ShapeServiceMaxRetrieveCount.ToString());
                }
                else
                {
                    serviceDAL.GetRegionListShapes(myXamlMainMap.ZoomLevel, mapLayer.layerID, newRegionsIDsWithoutShapes);
                }
            }
            else
            {
                mapLayer.ShowLayer();
            }
            }
        }



        public void OnServiceDALGetRegionListShapesCompleted(RegionsShapesSTO sto)
        {
            var mapLayer = (from lyr in mapLayerList
                            where lyr.layerID == sto.layerID
                            select lyr).First();
            if (null == mapLayer)
            {
                Log.HandleInternalError("OnServiceDALGetRegionIDsListShapesCompleted: unable to find returned layerID [" + sto.layerID + "] in known map layers");
            }
            else
            {
                byte[] decompressed = CompressionManager.DecompressToByteArray((byte[])sto.compressedRegionList);
                List<RegionDTO> regionList = (List<RegionDTO>)SerializationManager.Deserialize(typeof(List<RegionDTO>), decompressed);

                // the service doesn't know about the BingMapsShapes, until then, need to convert from the mapMultiShapeDTO to the mapMultiShape
                var mapRegionList = (from rgn in regionList
                                         select new MapRegionDTO(rgn.regionID, rgn.name
                                                        , MapShapeTools.BuildShape(rgn.regionID, MapShapeTools.ConvertServiceDTOtoLibSvDTO(rgn.mapMultiShapeDTO))
                                                        , rgn.centerLngX
                                                        , rgn.centerLatY
                                                        , rgn.boundingBox.northWest.latitude
                                                        , rgn.boundingBox.northWest.longitude
                                                        , rgn.boundingBox.southEast.latitude
                                                        , rgn.boundingBox.southEast.longitude)
                                         ).ToList(); 
                mapLayer.LoadRegions(mapRegionList, null, regionLabel);

                var regionIDList = (from rgn in regionList
                                    select rgn.regionID).ToList();

                serviceDAL.GetRegionListData(mapLayer.layerID, regionIDList);
            }

        }

        public void OnServiceDALGetRegionListDataCompleted(LayerRegionsDataSTO sto)
        {
            var mapLayer = (from lyr in mapLayerList
                where lyr.layerID == sto.layerID
                select lyr).First();
            if (null == mapLayer)
            {
                Log.HandleInternalError("OnServiceDALGetRegionListDataCompleted: unable to find returned layerID [" + sto.layerID + "] in known map layers");
            }
            else
            {
                mapLayer.LoadRegionsData(sto.regionsDataList);
            }
        }
    }
}
